package com.swipemaster.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.swipemaster.R;
import com.swipemaster.achievements.AchievementsManager;
import com.swipemaster.common.Constants;
import com.swipemaster.common.Coords;
import com.swipemaster.levels.generator.LevelGenerator;

/**
 * Starting point of application.
 */
public class MainActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		setGlobalScreenDimensions();
		
		// TODO: When all levels are done, remove this line.
		LevelGenerator.generate("survival", this);
		
		addButtons();
	}

	private void addButtons()
	{
		Button playLevels = (Button) findViewById(R.id.play_levels);
		Button playChallenge = (Button) findViewById(R.id.play_challenge);
		Button playSurvival = (Button) findViewById(R.id.play_survival);
		Button achievements = (Button) findViewById(R.id.achievements);
		Button exit = (Button) findViewById(R.id.exit);
		
		playLevels.setOnClickListener(getListener(PlayLevelsActivity.class));
		playChallenge.setOnClickListener(getListener(PlaySurvivalActivity.class));
		playSurvival.setOnClickListener(getListener(PlaySurvivalActivity.class));
		achievements.setOnClickListener(getListener(AchievementsActivity.class));
		exit.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				finish();
			}
		});
	}

	private void setGlobalScreenDimensions()
	{
		Display display = getWindowManager().getDefaultDisplay(); 
		Constants.screenWidth = display.getWidth(); 
		Constants.screenHeight = display.getHeight();
		Constants.screenDiagonal = Coords.pitagoras(Constants.screenWidth, Constants.screenHeight);
		Constants.screenRatio = (double) Constants.screenWidth / Constants.screenHeight;
	}
	
	private View.OnClickListener getListener(final Class<?> cls)
	{
		return new OnClickListener() 
		{
			public void onClick(View v)
			{
				Intent intent = new Intent(getApplicationContext(), cls);
	            startActivity(intent);
			}
		};
	}
	
	@Override
	protected void onPause()
	{
		super.onStop();
		AchievementsManager.saveAchievements(this);
	}
}
